<?php
!defined('IN_HDWIKI') && exit('Access Denied');

class control extends base{
	
	function control(& $get,& $post){
		$this->base( & $get,& $post);
		$this->load('user');
		$this->load('usergroup');
		eval($this->plugin["ucenter"]["hooks"]["include"]);
	}
	
	function doboxlogin(){
		@header('Content-type: text/html; charset='.WIKI_CHARSET);
		if(isset($this->setting['checkcode'])){
			$this->view->assign('checkcode',$this->setting['checkcode']);
		}else{
			$this->view->assign('checkcode',"0");
		}
		
		if (!isset($this->setting['name_max_length'])) {$this->setting['name_max_length'] = 15;}
		$this->view->assign('maxlength',$this->setting['name_max_length']);
		$this->view->display('loginbox');
	}
	
 	function doregister(){
		if ($this->user['islogin']){
			$this->header('');
		}
		if(!isset($this->post['submit'])){
			if ('0' === $this->setting['allow_register']){
				$this->message($this->setting['close_register_reason'],'',0);
			}
			
			if (isset($this->setting['register_least_minute']) && !$_ENV['user']->check_ip($this->ip)){
				$msg = $this->view->lang['registerTip9'];
				$msg = str_replace('30', $this->setting['register_least_minute'], $msg);
				$this->message($msg,'',0);
			}
			//passport
			if(defined('PP_OPEN')&&PP_OPEN){
				if(PP_TYPE=='server'){
					$this->view->assign('forward',$this->forward);
				}else if(PP_TYPE=='client'){
					$regurl = PP_API.PP_REG;
					header('location:'.$regurl.(false===strpos($regurl,'?')?'?':'&').'forward='.$this->forward);
					exit;
				}
			}
			
			if(isset($this->setting['checkcode'])){
				$this->view->assign('checkcode',$this->setting['checkcode']);
			}else{
				$this->view->assign('checkcode',"0");
			}
			
			if (!isset($this->setting['name_min_length'])) {$this->setting['name_min_length'] = 3;}
			if (!isset($this->setting['name_max_length'])) {$this->setting['name_max_length'] = 15;}
			$this->view->assign('minlength',$this->setting['name_min_length']);
			$this->view->assign('maxlength',$this->setting['name_max_length']);
			$loginTip2 = $this->view->lang['loginTip2'];
			$loginTip2 = str_replace('3',$this->setting['name_min_length'],$loginTip2);
			$loginTip2 = str_replace('15',$this->setting['name_max_length'],$loginTip2);
			$this->view->assign('loginTip2',$loginTip2);
			
			$this->view->display('register');
		}else{
			$username=trim($this->post['username']);
			$password=$this->post['password'];
			$repassword=$this->post['repassword'];
			$email=trim($this->post['email']);
			if(isset($this->setting['checkcode'])){
				if($this->setting['checkcode']!=3){
					$code=trim($this->post['code']);
				}else{
					$code="3";
				}
			}else{
				$code=$this->post['code'];
			}
			$error=$this->docheck($username,$password,$repassword,$email,$code);
			if($error===true){
				eval($this->plugin["ucenter"]["hooks"]["register"]);
				$uid=$_ENV['user']->add_user($username, md5($password), $email,$this->time,$this->ip);
				if($uid){
					$_ENV['user']->refresh_user($uid);
					$_ENV['user']->add_credit($this->user['uid'],'user-register',$this->setting['credit_register']);
					$this->view->assign('user',$this->user);
					$this->view->assign('newpms',$this->newpms($this->user['uid']));
					
					//passport
					if(defined('PP_OPEN') && PP_OPEN && PP_TYPE=='server'){
						$userarr=$this->user;
						if(PP_NAME=='PHPCMS'){
							$userarr['password']=$this->post['password'];
						}
						$_ENV['user']->pp_interface('login',$userarr,$this->post['forward']);
					}
					
					$this->message($this->view->lang['registerSuccess']."<b>$username</b>",'index.php',0);
				}else{
					$this->message($this->view->lang['registerFaild'],'',0);
				}
			}else{
				$this->view->assign('error',$error);
				$this->view->assign('username',$username);
				$this->view->assign('email',$email);
				$this->view->assign('code',$code);
				$this->view->display('register');
			}
		}
	}

	function dologin(){
		//passport
		if(defined('PP_OPEN')&&PP_OPEN){
			if(PP_TYPE=='server'){
				$pos=strpos($_SERVER['QUERY_STRING'],'?');
				if($pos!==false){
					parse_str(substr($_SERVER['QUERY_STRING'],$pos+1),$userdb);
					$userdb['submit']=1;
					$this->post=$userdb;
				}
			}
		}
		
		if(!isset($this->post['submit'])){
			if(isset($this->setting['checkcode'])){
				$this->view->assign('checkcode',$this->setting['checkcode']);
			}else{
				$this->view->assign('checkcode',"0");
			}
			$_ENV['user']->add_referer();
			//passport
			if(defined('PP_OPEN')&&PP_OPEN){
				if(PP_TYPE=='server'){
					$this->view->assign('forward',$this->forward);
				}else if(PP_TYPE=='client'){
					$loginurl = PP_API.PP_LOGIN;
					header('location:'.$loginurl.(false===strpos($loginurl,'?')?'?':'&').'forward='.$this->forward);
					exit;
				}
			}
			
			if (!isset($this->setting['name_max_length'])) {$this->setting['name_max_length'] = 15;}
			$this->view->assign('maxlength',$this->setting['name_max_length']);
			$this->view->display('login');
			exit;
		}
 		$username=$this->post['username'];
		if (WIKI_CHARSET == 'GBK' && $this->post['submit'] == 'ajax'){
			$username=string::hiconv($username);
			@header('Content-type: text/html; charset='.WIKI_CHARSET);
		}
		
	    $password=md5($this->post['password']);
		if(isset($this->setting['checkcode'])){
			if($this->setting['checkcode']!=3){
		    	$code=$this->post['code'];
				$error=$this->docheckcode($code,1);
		    }else{
		    	$error = "OK";
		   	}
		}else{
			$code=$this->post['code'];
			$error=$this->docheckcode($code,1);
		}
		//passport
		/*
		if(defined('PP_OPEN')&&PP_OPEN){
			if(PP_TYPE=='client'){
				$loginurl = PP_API.PP_LOGIN;
				header('location:'.$loginurl.(false===strpos($loginurl,'?')?'?':'&')."username=$username&password=".$this->post['password']."&dosubmit=1&forward=".$this->forward);
				exit;
			}
		}*/
		
		if(strcmp($error,'OK')==0){
			$user=$_ENV['user']->get_user("username",$username);
			if (is_array($user)
				&& $this->setting['close_website'] === '1'
				&& $user['groupid'] != 4
			){
				
				@header('Content-type: text/html; charset='.WIKI_CHARSET);
				exit($this->setting['close_website_reason']);
			}
			
			eval($this->plugin["ucenter"]["hooks"]["login"]);
			if(!empty($msg) && $msg != 'OK'){
				exit('(UC)');
			}
			if(is_array($user)&&($password==$user['password'])){
				$lasttime=$user['lasttime'];
				if($this->time>($lasttime+24*3600)){
					$_ENV['user']->add_credit($user['uid'],'user-login',$this->setting['credit_login']);
				}
				$_ENV['user']->update_user($user['uid'],$this->time,$this->ip);
				$_ENV['user']->refresh_user($user['uid']);
				//passport login
				if(defined('PP_OPEN') && PP_OPEN && PP_TYPE=='server'){
					if(PP_NAME=='PHPCMS'){
						$user['password']=$this->post['password'];
					}
					$_ENV['user']->pp_interface('login',$user,$this->post['forward']);
				}
				if ($this->post['submit'] == 'ajax'){
					echo 'OK';
				} else{
					$this->view->assign('user',$this->user);
					$this->view->assign('newpms',$this->newpms($this->user['uid']));
					$this->view->assign('adminlogin',$this->checkable('admin_main-login') );
					$this->message($this->view->lang['loginSuccess'],$_ENV['user']->get_referer() ,0);
				}
			}else{
				if ($this->post['submit'] == 'ajax'){
					echo $this->view->lang['userInfoError'];
				} else{
					$this->message($this->view->lang['userInfoError'],'BACK',0);
				}
				
			}
		}else{
			if ($this->post['submit'] == 'ajax'){
				echo $error;
			} else{
				$this->message($error, 'BACK',0);
			}			
		}
	}

	function docheck($username,$password,$repassword,$email,$code){
		$error=$this->docheckusername($username,1);
		if(strpos($error,"OK")===false)
			return $error;
		$error=$this->docheckpassword($password,$repassword);
		if(true!==$error)
			return $error;
		$error=$this->docheckemail($email,1);
		if(strpos($error,"OK")===false)
			return $error;
		if($code!=3){
			$error=$this->docheckcode($code,1);
		}else{
			$error = "OK";
		}
		if(strpos($error,"OK")===false){return $error;}
		return true;
	}

 	function docheckusername($username='',$type=0){
		if( empty($username) ){
			$username=string::hiconv($this->post['username']);
		}
		if( ! preg_match("/^[\w\s\d_\x80-\xff]+$/is", $username) ){
			$msg=$this->view->lang['registerTip8'];
		}
		
		if (!$_ENV['user']->check_name($username)){
			$this->message($this->view->lang['registerTip8'],'',2);
		}
		
		if((bool)$_ENV['user']->get_user("username",$username)){
			$msg=$this->view->lang['registerTip6'];
			eval($this->plugin["ucenter"]["hooks"]["checkname"]);
		}else{
			$msg="OK";
			eval($this->plugin["ucenter"]["hooks"]["checkname"]);
		}
		if($type==1){
			return $msg;
		}else{
			$this->message($msg,'',2);
		}
	}

 	function docheckcode($code='',$type=0){
 		if( empty($code) ){
 			$code=$this->post['code'];
 		}
 		$msg=$this->view->lang['codeError'];
		if(strtolower($code)==$this->user['code']){
		 	$msg="OK";
		}
		if($type==1){
			return $msg;
		}else{
			$this->message($msg,'',2);
		}
	}

 	function docheckpassword($password,$repassword){
 		if($password!=$repassword)
 			return $this->view->lang['editPassTip3'];
		$lenmax=strlen($password);
		if(0 == $lenmax || $lenmax>32){
			return $this->view->lang['editPassTip1'];
		}else{
			return true;
		}
	}

	function docheckoldpass(){
		$oldpass=md5($this->post['oldpass']);
		if($oldpass==$this->user['password']){
			$this->message('OK','',2);
		}else{
			$this->message($this->view->lang['oldPassError'],'',2);
		}
	}

 	function docheckemail($email='',$type=0){
		if($email==''){
			$email=$this->post['email'];
		}
		$lenmax=strlen($email);
		if((bool)$_ENV['user']->get_user("email",$email)){
			$msg=$this->view->lang['registerTip7'];
		}elseif($lenmax<6){
			$msg=$this->view->lang['getPassTip2'];
		}else{
			$msg="OK";
			eval($this->plugin["ucenter"]["hooks"]["checkemail"]);
		}
		if($type==1){
			return $msg;
		}
		$this->message($msg,'',2);
	}

 	function dologout(){
 		//passport logout
		if(defined('PP_OPEN') && PP_OPEN){
			if(PP_TYPE=='client'){
				$logouturl = PP_API.PP_LOGOUT;
				header('location:'.$logouturl.(false===strpos($logouturl,'?')?'?':'&').'forward='.$this->forward);
				exit;
			}
		}
		$synlogout="";
		$_ENV['user']->user_logout($this->hgetcookie('sid'));
		
		//passport logout
		if(defined('PP_OPEN') && PP_OPEN){
			if(PP_TYPE=='server'){
				$_ENV['user']->pp_interface('logout',$this->user);
			}
		}
		
		eval($this->plugin["ucenter"]["hooks"]["logout"]);
		$refer=substr($_SERVER['QUERY_STRING'],12);
		if( empty($refer) ){
			$refer=$this->setting['site_url'] ?$this->setting['site_url'] :'index.php';
		}
		$this->hsetcookie('style','',0);
		$this->message($this->view->lang['logoutSuccess'].$synlogout, urldecode($refer), 0);
	}

	function doprofile(){
		if($this->user['birthday']){
			$this->user['birthday'] = date('Y-m-d',$this->user['birthday']);
		}else{
			$this->user['birthday']='';
		}
		eval($this->plugin["ucenter"]["hooks"]["iscredit"]);
		$this->user['editorstar']=$_ENV['usergroup']->get_star($this->user['stars']);
		$this->user['regtime'] = $this->date($this->user['regtime']);
		$creditDetail = $_ENV['user']->get_credit($this->user['uid']);
		eval($this->plugin['ucenter']['hooks']['pro_user_image']);
		$this->user['credits']= $creditDetail['creditTotal'];
		//passport logout
		if(defined('PP_OPEN') && PP_OPEN){
			if(PP_TYPE=='server'){
				$_ENV['user']->pp_interface('logout',$this->user);
			}
		}
		$this->view->assign('creditDetail',$creditDetail);
		$this->view->assign('user',$this->user);
		$this->view->assign('randnum',rand(0,100000));
		$this->view->display('profile');
	}

	function doeditprofile(){
		if(isset($this->post['submit'])){
			$gender = intval($this->post['gender']);
			$birthday = strtotime($this->post['birthday']);
			$location = htmlspecialchars(string::hiconv($this->post['location']));
			$signature = htmlspecialchars(string::hiconv($this->post['signature']));
			$_ENV['user']->set_profile($gender,$birthday,$location,$signature,$this->user['uid']);
		}else{
			if(0 == $this->user['birthday']){
				$birthday = '';
			}else{
				$birthday=$this->setting['time_offset']*3600+$this->setting['time_diff']*60+$this->user['birthday'];
				$birthday = date('Y-m-d',$birthday);
			}
			
			$this->view->assign('birthday',$birthday);
			$this->view->display('editprofile');
		}
	}

	function doeditpass(){
		if(isset($this->post['submit'])){
			$oldpass = $this->post['oldpass'];
			$newpass = $this->post['newpass'];
			$renewpass = $this->post['renewpass'];
			$oldpass=md5($oldpass);
			if(strlen($newpass)<1){
				$this->message($this->view->lang['editPassTip1'],"BACK",0);
			}
			if($oldpass==$this->user['password']){
				if($newpass==$renewpass){
					eval($this->plugin["ucenter"]["hooks"]["editpass"]);
					$_ENV['user']->update_field('password',md5($newpass),$this->user['uid']);
					$this->view->assign('message',$this->view->lang['viewDocTip1']);
					$this->view->display('editpass');
					exit;
				}else{
					$this->message($this->view->lang['editPassTip3'],'BACK',0);
				}
			}else{
				$this->message($this->view->lang['oldPassError'],'BACK',0);
			}
		}
		$this->view->display('editpass');
	}

	function doeditimage(){
		if(isset($this->post['submit'])){
			$image = $_FILES['image'];
			$extname=file::extname($image['name']);
			if( util::isimage($extname) ){
				$destfile = 'uploads/userface/'.($this->user['uid']%10).'/'.$this->user['uid'].'.'.$extname;
				$result = file::uploadfile($image,$destfile);
				if($result['result']){
					util::image_compress($destfile,NULL,90,90);
					$_ENV['user']->update_field("image",$destfile,$this->user['uid']);
					$this->view->assign('message',$this->view->lang['editImageSuccess']);
				}else{
					$this->view->assign('message',$result['msg']);
				}
			}else{
				$this->view->assign('message',$this->view->lang['editImageIllegal']);
			}
		}
		eval($this->plugin['ucenter']['hooks']['edit_user_image']);
		$this->view->display('editimage');
	}

	function dogetpass(){
		if(isset($this->get[2])){
			$uid=$this->get[2];
			$encryptstring=$this->get[3];
			$idstring=$_ENV['user']->get_idstring_by_uid($uid,$this->time);

			if($idstring==$encryptstring){
				$this->view->assign('uid',$uid);
				$this->view->assign('encryptstring',$encryptstring);
				$this->view->display('resetpass');
				exit;
			}else{
				$this->message($this->view->lang['resetPassMessage'], $this->setting['site_url'] ,0);
			}
		}elseif(isset($this->post['verifystring'])){
			$uid=$this->post['uid'];
			$encryptstring=$this->post['verifystring'];
			$idstring=$_ENV['user']->get_idstring_by_uid($uid,$this->time);
			if($idstring==$encryptstring){
				$newpass = $this->post['password'];
				$renewpass = $this->post['repassword'];
				$error=$this->docheckpassword($newpass,$renewpass);
				if($error===true){
					eval($this->plugin["ucenter"]["hooks"]["getpass"]);
					$_ENV['user']->update_field('password',md5($newpass),$uid);
					$_ENV['user']->update_getpass($uid);
					$this->message($this->view->lang['resetPassSucess'],'index.php?user-login',0);
				}else{
				  $this->message($error,'BACK',0);
				}
			}else{
				$this->message($this->view->lang['resetPassMessage'], $this->setting['site_url'] ,0);
			}
		}
		if(!isset($this->post['submit'])){
			if(isset($this->setting['checkcode'])){
				$this->view->assign('checkcode',$this->setting['checkcode']);
			}else{
				$this->view->assign('checkcode',"0");
			}
			$this->view->display('getpass');
			exit;
		}
		$email=$this->post['email'];
		if(isset($this->setting['checkcode'])){
			if($this->setting['checkcode']!=3){
				$code=$this->post['code'];
				$error=$this->docheckcode($code,1);
			}else{
				$error = "OK";
			}
		}else{
			$code=$this->post['code'];
			$error=$this->docheckcode($code,1);
		}
		if("OK" != $error){
			$this->message($error,'BACK',0);
		}
		$user=$_ENV['user']->get_user("email",$email);
		if(!(bool)$user){
			$this->message($this->view->lang['noMail'],'BACK',0);
		}else{
			$timetemp=date("Y-m-d h:i:s",$this->time);
			while (($verification = rand() % 10000) < 1000);
			$encryptstring=md5($this->time.$verification);
			$reseturl=$this->setting['site_url']."/index.php?user-getpass-".$user['uid'].'-'.$encryptstring;
			$_ENV['user']->update_getpass($user['uid'],$encryptstring);
			$mail_subject = $this->setting['site_name'].$this->view->lang['getPass'];
			$mail_message = $this->view->lang['resetPassMs1'].$user['username'].$this->view->lang['resetPassMs2'].$timetemp.$this->view->lang['resetPassMs3']."<a href='".$reseturl."' target='_blank'>".$reseturl."</a>".$this->view->lang['resetPassMs4'].$this->setting['site_name'].$this->view->lang['resetPassMs5'].$this->setting['site_name'].$this->view->lang['resetPassMs6'];
			$adminemail = $_ENV['user']->get_admin_email();
			$_ENV['user']->send_email($email,$mail_subject,$mail_message,$adminemail);
			$this->message($this->view->lang['emailSucess'],'index.php?user-login',0);
		}
	}

	function docode(){
		if(isset($this->setting['checkcode'])){
			$codecase = $this->setting['checkcode'];
		}else{
			$codecase = 0;
		}
 		$code=util::random(4,$codecase);
		if(function_exists("imagecreate")){
			$_ENV['user']->save_code(strtolower($code),$this->user['sid']);
			util::makecode($code);
		}else{
			$_ENV['user']->save_code('abcd',$this->user['sid']);
			header('location:style/default/vdcode.jpg');
		}
	}

	function dospace(){
		$uid = $this->get[2];
		$type=(isset($this->get[3]))?intval($this->get[3]):0;
		$page = max(1, intval($this->get[4]));
		$num = isset($this->setting['list_prepage'])?$this->setting['list_prepage']:20;
		$start_limit = ($page - 1) * $num;
		
		$_ENV['user']->update_field('views',1,$uid,0);
		if($uid == $this->user['uid']){
			$spaceuser = $this->user;
			$spaceuser['views']++;
			$spaceuser['editorstar'] = $_ENV['usergroup']->get_star($spaceuser['stars']);
			
		}elseif(is_numeric($uid)){
			$spaceuser = $_ENV['usergroup']->get_groupinfo($uid);
		}else{
			$spaceuser = $_ENV['usergroup']->get_groupinfo($uid,'u.username');
			$uid=$spaceuser['uid'];
		}
		if(!(bool)$spaceuser){
			$this->message($this->view->lang['loginTip3'],'BACK',0);
		}
		eval($this->plugin['ucenter']['hooks']['space_user_image']);
		$spaceuser['regtime'] = $this->date($spaceuser['regtime']);
		$doccount=($type)? $spaceuser['edits'] : $spaceuser['creates'];
		$doclist = $_ENV['user']->space($uid,$type,$start_limit,$num);
		$departstr=$this->multi($doccount, $num, $page,"user-space-$uid-$type");
		
		$this->view->assign('departstr',$departstr);
		$this->view->assign('type',$type);
		$this->view->assign('doclist',$doclist);
		$this->view->assign('spaceuser',$spaceuser);
		$this->view->display('space');
	}

	function doclearcookies(){
		$cookiepre=$this->setting['cookie_pre']?$this->setting['cookie_pre']:'hd_';
		$lenth=strlen($cookiepre);
		if(is_array($_COOKIE)) {
			foreach ($_COOKIE as $key => $val) {
				if(substr($key,0,$lenth)==$cookiepre){
					$this->hsetcookie(substr($key,$lenth), '',0);
				}
			}
		}
	 	$this->header();
	}
}
?>